package cn.edu.hznu;

import cn.edu.hznu.dao.UserMapper;
import cn.edu.hznu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * Created by wjj on 2020/5/25
 */
public class testUser {
    public static void main(String[] args) throws  Exception{
        //读取配置文件
        InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
        //获取SessionFactory
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(in);
        //生产sqlsession
        SqlSession sqlSession = sessionFactory.openSession(true);
        //创建dao的代理对象
        UserMapper dao = sqlSession.getMapper(UserMapper.class);
        //使用代理对象操作
        List<User> users = dao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        //释放资源
        sqlSession.close();
        in.close();
    }
}
